@echo off
echo ========================================
echo Fix Encoding and Build Installer
echo ========================================
echo.

:: Step 1: Check if build exists
echo [Step 1/4] Checking build output...
if not exist "build\bin\Release\LimeReader.exe" (
    echo [ERROR] Build not found!
    echo Please run: build.bat
    pause
    exit /b 1
)
echo [OK] Build found

:: Step 2: Set environment for UTF-8
echo [Step 2/4] Setting UTF-8 environment...
chcp 65001 >nul
set "PYTHONIOENCODING=utf-8"
echo [OK] Environment set

:: Step 3: Use windeployqt to gather dependencies
echo [Step 3/4] Gathering Qt dependencies...
set "QT_DIR=C:\Qt\6.9.2\mingw_64"
if not exist "%QT_DIR%" (
    echo [ERROR] Qt not found at: %QT_DIR%
    echo Please update QT_DIR in this script
    pause
    exit /b 1
)

"%QT_DIR%\bin\windeployqt.exe" --release --no-compiler-runtime --no-system-d3d-compiler --no-opengl-sw "build\bin\Release\LimeReader.exe"
if errorlevel 1 (
    echo [WARNING] windeployqt failed, continuing anyway...
)
echo [OK] Qt dependencies gathered

:: Step 4: Compile installer with English version
echo [Step 4/4] Compiling installer...
set "ISCC="
if exist "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" (
    set "ISCC=C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
)
if exist "C:\Program Files\Inno Setup 6\ISCC.exe" (
    set "ISCC=C:\Program Files\Inno Setup 6\ISCC.exe"
)

if "%ISCC%"=="" (
    echo [ERROR] Inno Setup not found
    echo Please install Inno Setup: https://jrsoftware.org/isinfo.php
    pause
    exit /b 1
)

echo Using English-only installer to avoid encoding issues...
"%ISCC%" "LimeReader_EN.iss"
if errorlevel 1 (
    echo [ERROR] Installer compilation failed
    pause
    exit /b 1
)

echo.
echo ========================================
echo SUCCESS! Installer created!
echo ========================================
echo.
echo Output: installer\LimeReader_Setup_v2.7.2.exe
echo.
echo This version uses English interface to avoid encoding issues.
echo.
if exist "installer\LimeReader_Setup_v2.7.2.exe" (
    echo Installer size:
    dir "installer\LimeReader_Setup_v2.7.2.exe" | find "LimeReader"
)
echo.
pause

